package com.ruoyi.supplier.controller;
import java.util.HashMap;
import java.util.Date;
import java.util.Map;

import com.ruoyi.system.domain.T3rdstock;
import com.ruoyi.system.domain.VO.TOrderDetailVO;

import java.beans.Transient;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.service.SnowflakeIdGenerator;
import com.ruoyi.supplier.domain.DeliverRequest;
import com.ruoyi.system.service.IT3rdstockService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.supplier.domain.TOrders;
import com.ruoyi.supplier.service.ITOrdersService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单信息Controller
 * 
 * @author ruoyi
 * @date 2024-05-22
 */
@RestController
@RequestMapping("/supplier/orders")
public class TOrdersController extends BaseController
{
    @Autowired
    private ITOrdersService tOrdersService;

    @Autowired
    private IT3rdstockService t3rdstockService;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    /**
     * 查询订单信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TOrders tOrders)
    {
        startPage();
        List<TOrders> list = tOrdersService.selectTOrdersList(tOrders);
        return getDataTable(list);
    }

    /**
     * 导出订单信息列表
     */
    @Log(title = "订单信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TOrders tOrders)
    {
        List<TOrders> list = tOrdersService.selectTOrdersList(tOrders);
        ExcelUtil<TOrders> util = new ExcelUtil<TOrders>(TOrders.class);
        util.exportExcel(response, list, "订单信息数据");
    }

    /**
     * 获取订单信息详细信息
     */
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") String orderId)
    {
        return success(tOrdersService.selectTOrdersByOrderId(orderId));
    }

    /**
     * 新增订单信息
     */
    @Log(title = "订单信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TOrders tOrders)
    {
        tOrders.setOrderId(String.valueOf(snowflakeIdGenerator.nextId()));
        return toAjax(tOrdersService.insertTOrders(tOrders));
    }

    /**
     * 修改订单信息
     */
    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TOrders tOrders)
    {
        return toAjax(tOrdersService.updateTOrders(tOrders));
    }

    /**
     * 发货
     */
    @Transactional
    @Log(title = "订单信息", businessType = BusinessType.UPDATE)
    @PostMapping("/deliver")
    public AjaxResult deliver(@RequestBody DeliverRequest deliverRequest)
    {
        String orderId = deliverRequest.getOrderId();
        List<TOrderDetailVO> goodsList = deliverRequest.getGoodsList();
        String warehouseId = deliverRequest.getWarehouseId();

        for (TOrderDetailVO tOrderDetailVO : goodsList) {
            Long amount = tOrderDetailVO.getAmount();
            String goodsId = tOrderDetailVO.getGoodsId();
            String goodsName = tOrderDetailVO.getGoodsName();
            T3rdstock t3rdstock = new T3rdstock();
            t3rdstock.setGoodsId(goodsId);
            t3rdstock.setWarehouseId(warehouseId);
            List<T3rdstock> t3rdstocks = t3rdstockService.selectT3rdstockByGoodIdAndWarehouseId(t3rdstock);
            if (t3rdstocks.isEmpty() || t3rdstocks.get(0).getSupplierStock() < amount) {
                return error(goodsName +" 库存不足");
            }
        }

        for (TOrderDetailVO tOrderDetailVO : goodsList) {
            Long amount = tOrderDetailVO.getAmount();
            String goodsId = tOrderDetailVO.getGoodsId();
            String goodsName = tOrderDetailVO.getGoodsName();
            T3rdstock t3rdstock = new T3rdstock();
            t3rdstock.setGoodsId(goodsId);
            t3rdstock.setWarehouseId(warehouseId);
            t3rdstock.setSupplierStock(amount*-1);
            int res = t3rdstockService.updateT3rdstockByAdd(t3rdstock);
            if (res == 0) {
                return error(goodsName + "出库失败");
            }
        }

        TOrders tOrders = new TOrders();
        tOrders.setOrderId(orderId);
        tOrders.setStatus("3");
        int res = tOrdersService.updateTOrders(tOrders);
        if (res == 0) {
            return error("出库失败");
        }

        return success("出库成功");
    }

    /**
     * 删除订单信息
     */
    @Log(title = "订单信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable String[] orderIds)
    {
        return toAjax(tOrdersService.deleteTOrdersByOrderIds(orderIds));
    }
}
